#!/usr/bin/env python
# -*- coding: utf-8 -*-

# forever mako

import urllib,httplib

apikey="9f62a5451309609e14345572bf552550"
PhoneNumber="18011700163"


class Message:
    def __init__(self):
        global apikey
        self.apikey=apikey
        
        
    def SendTemplate(self,templatetext,mobile,param):
        for key in param:
            ReplaceStr='#'+str(key)+'#'
            templatetext=templatetext.replace(ReplaceStr,param[key])
        return self.SendSms(self.apikey,templatetext,mobile)
        

    # apikey:成功注册后登录云片官网,进入后台可查看
    # text:需要使用已审核通过的模板或者默认模板
    # mobile:接收的手机号,仅支持单号码发送
    def SendSms(self,apikey, text, mobile):
        #服务地址
        sms_host = "sms.yunpian.com"
        #端口号
        port = 443
        #版本号
        version = "v2"
        #智能匹配模板短信接口的URI
        sms_send_uri = "/" + version + "/sms/single_send.json"
        params = urllib.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile})
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
        conn.request("POST", sms_send_uri, params, headers)
        response = conn.getresponse()
        response_str = response.read()
        conn.close()
        return response_str
    
# 定制好的消息
class NotifyMessage:
    def __init__(self):
        global PhoneNumber
        self.Message=Message()
        self.templatetext="您关注的#item#到货了，现存数量#count#，请登录小世界进行操作。"
        self.PhoneNumber=PhoneNumber
        
    def Send(self,item,count):
        param={}
        param["item"]=item
        param["count"]=count
        return self.Message.SendTemplate(self.templatetext, self.PhoneNumber, param)
   
NotifyMessage=NotifyMessage()     



if __name__=='__main__':
    NotifyMessage.Send("000810.sz","16.23L")
    print("123")
